package mashibing.class9;

import pojo.ListNode;

/**
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-15
 */
public class Class9_2_LinkMidNextTest {

    // 奇偶链表返回中间节点(偶数为下中点)
    public static int getMidListNode(ListNode root) {
        if (root == null) {
            return -1;
        }
        // 慢节点1步，快节点2步
        ListNode slowNode = root;
        ListNode fastNode = root.next;
        int ans = slowNode.val;
        while (fastNode != null) {
            ans = slowNode.next.val;
            fastNode = fastNode.next;
            if (fastNode != null) {
                slowNode = slowNode.next;
                fastNode = fastNode.next;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1, null);
        System.out.println(getMidListNode(listNode1));
        ListNode listNode2 = new ListNode(1, new ListNode(2, null));
        System.out.println(getMidListNode(listNode2));
        ListNode listNode5 = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, null)))));
        System.out.println(getMidListNode(listNode5));
        ListNode listNode6 = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6, null))))));
        System.out.println(getMidListNode(listNode6));
    }
}
